   package com.javalong.affairs.module.notifything.adapter;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;

import com.javalong.affairs.R;
import com.javalong.affairs.base.adapter.BaseAdapter;
import com.javalong.affairs.base.model.User;

public class ReceiverAdapter extends BaseAdapter {
	private List<User> selectedUsers;
	private Type type;

	public ReceiverAdapter(Context context, List<User> userList, Type type) {
		super(context, userList);
		selectedUsers = new ArrayList<User>();
		this.type = type;
	}

	public List<User> getSelectedUsers() {
		switch (type) {
		case RECEIVER:
			if (selectedUsers.size() > 0) {
				User user = selectedUsers.get(0);
				if (user.id == -1) {
					selectedUsers.remove(0);
				}
			}
			break;
		}
		return selectedUsers;
	}

	/*
	 * @Override public void notifyDataSetChanged() {
	 * super.notifyDataSetChanged(); selectedUsers.clear(); }
	 */

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final User user = (User) getItem(position);
		ViewHolder holder;
		if (convertView != null) {
			holder = (ViewHolder) convertView.getTag();
		} else {
			holder = new ViewHolder();
			convertView = mInflater.inflate(R.layout.item_receiver, null);
			holder.tv_user = (TextView) convertView.findViewById(R.id.tv_user);
			holder.cb_user = (CheckBox) convertView.findViewById(R.id.cb_user);
			convertView.setTag(holder);
		}

		switch (type) {
		case DELEGATE:
			holder.cb_user.setVisibility(View.GONE);
			break;
		case RECEIVER:
			if (position == 0) {
				checkAll(holder.cb_user);
			} else {
				holder.cb_user
						.setOnCheckedChangeListener(new OnCheckedChangeListener() {
							@Override
							public void onCheckedChanged(
									CompoundButton buttonView, boolean isChecked) {
								if (isChecked) {
									if (!isSelectedUser(user)) {
										selectedUsers.add(user);
									}
								} else {
									if (isSelectedUser(user)) {
										selectedUsers.remove(user);
									}
								}
							}
						});
			}
			break;
		}
		holder.tv_user.setText(user.name);
		if (isSelectedUser(user)) {
			holder.cb_user.setChecked(true);
		} else {
			holder.cb_user.setChecked(false);
		}
		return convertView;
	}

	private boolean isSelectedUser(User user) {
		for (User u : selectedUsers) {
			if (u.id == user.id) {
				return true;
			}
		}
		return false;
	}

	private void checkAll(CheckBox cb) {
		cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				if (isChecked) {
					selectedUsers.addAll((Collection<? extends User>) dataList);
				} else {
					selectedUsers.clear();
				}
				notifyDataSetChanged();
			}
		});
	}

	class ViewHolder {
		CheckBox cb_user;
		TextView tv_user;
	}

	public enum Type {
		DELEGATE, RECEIVER
	}

}
